﻿using AutoMapper;
using B.S.BasicData.Api.Read.Application.Command.Account;
using B.S.BasicData.Api.Read.Dtos;
using B.S.BasicData.Domain.Entities;
using B.S.BasicData.ErrorCode;
using B.S.BasicData.Infrastructure.Interface;
using MediatR;
using System.Collections.Generic;

namespace B.S.BasicData.Api.Read.Application.CommandHandler.Account
{
    public class GetAccountHandler : IRequestHandler<GetAccountCommand, ApiResult<List<AccountDto>>>
    {
        private readonly IBaseRepository<AccountModel> accRepo;
        private readonly IBaseRepository<RoleModel> reloRepo;
        private readonly IBaseRepository<DepartmentModel> departRepo;
        private readonly IMapper mapper;

        public GetAccountHandler(IBaseRepository<AccountModel> accRepo,
            IBaseRepository<RoleModel> reloRepo,
            IBaseRepository<DepartmentModel> departRepo,
            IMapper mapper
            )
        {
            this.accRepo = accRepo;
            this.reloRepo = reloRepo;
            this.departRepo = departRepo;
            this.mapper = mapper;
        }

        /// <summary>
        /// 查询账号列表的中介者处理方法
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Task<ApiResult<List<AccountDto>>> Handle(GetAccountCommand request, CancellationToken cancellationToken)
        {
            ApiResult<List<AccountDto>> result = new ApiResult<List<AccountDto>>();

            var accounts = accRepo.GetAll().Where(x=>!x.IsDeleted&&x.RoleId==1); //把所有账号取出来 select * fron accounts
            var roles = reloRepo.GetAll(); //取出所有角色
            var departs = departRepo.GetAll();  //取出所有部门

            if (!string.IsNullOrEmpty(request.Name)) {
                accounts = accounts.Where(x=>x.Name.Contains(request.Name));// select * fron accounts where name like '%name%'
            }

            if (!string.IsNullOrEmpty(request.DepartmentName))
            {
                departs = departs.Where(x=>x.DepartName.Contains(request.DepartmentName));
            }

            var linq = from a in accounts
                       join r in roles on a.RoleId equals r.Id
                       join d in departs on a.DepartmentId equals d.Id
                       select new AccountDto { 
                           //id没有
                       Id = a.Id,
                       Name = a.Name,
                       RealName = a.RealName,
                       Phone= a.Phone,
                       Card = a.Card,
                       //RoleId = a.RoleId,
                       RoleName = r.RoleName,
                       DepartmentName = d.DepartName,
                       DepartmentId = a.DepartmentId,
                       RoleId = a.RoleId
                       };
           
            result.Data = linq.ToList();
            result.Code = ApiEnums.Success;
            result.Msg = "查询成功!";
             
            return Task.FromResult(result);
        }
    }
}
